package middleware

import (
	"github.com/gin-gonic/gin"
	"jwgin/service"
)

// 权限控制
func RBAC() gin.HandlerFunc {
	return func(context *gin.Context) {
		user, bools := context.Get("UserID")
		if !bools {
			context.AbortWithStatusJSON(403, gin.H{"message": "非法操作"})
		}
		access, err := service.E.Enforce(user, context.Request.RequestURI, context.Request.Method)
		if err != nil || !access {
			context.AbortWithStatusJSON(403, gin.H{"message": "没有权限"})
		} else {
			context.Next()
		}
	}
}
